package com.cskaoyan.controller.wx_address_controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.vo.promotion_vo.BaseRespWithoutDataVO;
import com.cskaoyan.bean.wx_bo.SaveWxAddressBO;
import com.cskaoyan.bean.wx_vo.address.AddressListVO;
import com.cskaoyan.bean.wx_vo.address.DetailWxAddressVO;


import com.cskaoyan.service.wx_address_service.WxAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @description：
 * @author: 曹旭
 * @date: 2022/5/10 07:47
 */
@RestController
@RequestMapping("wx/address")
public class WxAddressController {
    @Autowired
    WxAddressService addressService;

    @RequestMapping("list")
    public BaseRespVo list() {
        AddressListVO addressListVO = addressService.list();
        return BaseRespVo.ok(addressListVO);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        DetailWxAddressVO detailWxAddressVO = this.addressService.detail(id);
        return BaseRespVo.ok(detailWxAddressVO);
    }

    /**
     *
     * @param saveWxAddressBO
     * @param bindingResult
     * @return Object
     * 返回值定为Object不太好,不过可以尽可能少改动代码,避免冲突
     * 最好让BaseRespVo extends BaseRespWithoutDataVO
     */
    @RequestMapping("save")
    public Object detail(@RequestBody @Validated SaveWxAddressBO saveWxAddressBO,
                         BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespWithoutDataVO.fail(401,"参数不对");
        }
        Integer id = this.addressService.detail(saveWxAddressBO);
        return BaseRespVo.ok(id);
    }

    @RequestMapping("delete")
    public BaseRespWithoutDataVO delete(@RequestBody Map map) {
        this.addressService.delete(map);
        return BaseRespWithoutDataVO.ok();
    }
}


